import {redirectToLogin} from '@/utils/redirectToLoginPage';
import Regexps from '@/utils/reg';
import type {ProFormInstance} from '@ant-design/pro-components';
import {ActionType, ModalForm, ProFormText} from '@ant-design/pro-components';
import {useRequest} from '@umijs/max';
import {message} from 'antd';
import React, {useEffect, useRef, useState} from 'react';
import {changePassword} from '@/pages/organization/user-list/service'
import {LicenseListItem} from "@/pages/license/data";
import {StorageListItem} from "@/pages/power/storage/data";
import {storageAdd, storageEdit} from "@/pages/power/storage/service";
import {FormValueType} from "@/pages/power/storage";

type ImportLicenseProps = {
    importModalVisible: boolean;
    onClose: () => void;
};
const handleImport = async (fields: LicenseListItem) => {
    const hide = message.loading('正在导入');

    try {
        await storageAdd({...fields});
        hide();
        message.success('导入成功');
        return true;
    } catch (error) {
        hide();
        message.error('导入失败请重试！');
        return false;
    }
};

const ImportLicense: React.FC<ImportLicenseProps> = (props) => {
    /** 分布更新窗口的弹窗 */
    const [importModalVisible, handleImportModalVisible] = useState<boolean>(false);
    const actionRef = useRef<ActionType>();

    return (
        <ModalForm
            title="LICENSE导入"
            width="550px"
            open={importModalVisible}
            onVisibleChange={handleImportModalVisible}
            onFinish={async (value) => {
                const success = await handleImport(value as LicenseListItem);
                if (success) {
                    handleImportModalVisible(false);
                    if (actionRef.current) {
                        actionRef.current.reload();
                    }
                }
            }}
            modalProps={
                {
                    destroyOnClose: true,
                }
            }
        >
        </ModalForm>
    );
};

export default ImportLicense;
